﻿#region

using System.Messaging;

#endregion

namespace NsbMonitorClientService.QueueManager
{
    public class ExploreMsMq : IExploreMsMq
    {
        private readonly IFetchQueueStatus _fetchQueueStatus;

        public ExploreMsMq(IFetchQueueStatus fetchQueueStatus)
        {
            _fetchQueueStatus = fetchQueueStatus;
        }

        public QueueStatus QueueStatus(string queuePath)
        {
            if (QueueNotExists(queuePath))
            {
                return null;
            }

            using (MessageQueue messageQueue = CreateMessageQueue(queuePath))
            {
                return _fetchQueueStatus.QueueStatus(messageQueue);
            }
        }

        private MessageQueue CreateMessageQueue(string queuePath)
        {
            if (QueueNotExists(queuePath))
            {
                return null;
            }

            MessageQueue messageQueue = new MessageQueue(queuePath);

            return messageQueue;
        }

        private bool QueueNotExists(string queuePath)
        {
            return !MessageQueue.Exists(queuePath);
        }
    }
}